package io.renren.modules.study.controller;


import io.renren.common.annotation.Login;
import io.renren.common.utils.R;
import io.renren.modules.study.entity.*;
import io.renren.modules.study.service.CoursePlanService;
import io.renren.modules.study.entity.CourseBody;
import io.renren.modules.study.entity.CourseScheduleEntity;
import io.renren.modules.schedule.entity.CourseBodyScheduleEntity;
import io.renren.modules.sys.dto.CollectDto;
import io.renren.modules.study.service.CourseService;
import io.renren.modules.study.service.MyCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试接口
 *
 * @author 薛冰
 * @email ztexuebing@126.com
 * @date 2017-03-23 15:47
 */
@RestController
@RequestMapping("/api/myCourse")
@Api(tags = "我的课程")
public class MyCourseController {

    @Autowired
    MyCourseService myCourseService;
    @Autowired
    CourseService courseService;
    @Autowired
    CoursePlanService coursePlanService;

    @Login
    @PostMapping("collect")
    @ApiOperation("收藏我的课程")
    public R collect(@RequestBody CollectDto c, @ApiIgnore @RequestAttribute("userId") Long userId) {
        System.out.println(userId);
        c.setUserId(userId);
        int code = 100;//保存成功
        String msg = "";
        CollectDto collectDto = myCourseService.getCollectionByCourseId(c);
//        CollectDto collectDto=myCourseService.getMyCourseByCourseId(c);
        if (collectDto == null) {
            myCourseService.saveCollection(c);
        }
        return R.ok(msg, code);
    }

    @Login
    @PostMapping("isCollect")
    @ApiOperation("根据课程获取是否收藏")
    public R isCollect(@RequestBody CollectDto c, @ApiIgnore @RequestAttribute("userId") Long userId) {
        System.out.println(userId);
        c.setUserId(userId);
        int code = 100;//已收藏
        String msg = "已收藏";
        CollectDto collectDto = myCourseService.getCollectionByCourseId(c);
        if (collectDto == null) {
            code = 101;//未收藏
            msg = "未收藏";
        }
        return R.ok(msg, code);
    }


    @Login
    @GetMapping("getCollectionList")
    @ApiOperation("获取收藏列表")
    public R collectionList(@ApiIgnore @RequestAttribute("userId") Long userId) {
        List list = myCourseService.getCollectionList(userId);
        return R.ok("", 100).put("data", list);
    }


    @Login
    @PostMapping("updateMyHistory")
    @ApiOperation("更新观看历史")
    public R updateMyHistory(@RequestBody CollectDto c, @ApiIgnore @RequestAttribute("userId") Long userId) {
        System.out.println(userId);
        c.setUserId(userId);
        CollectDto collectDto = myCourseService.getMyHistoryByCourseId(c);
//        CollectDto collectDto=myCourseService.getMyCourseByCourseId(c);
        if (collectDto == null) {
            myCourseService.saveHistory(c);
        } else {
            myCourseService.updateHistory(c);
        }
        return R.ok("", 100);
    }

    @Login
    @GetMapping("getMyHistoryList")
    @ApiOperation("获取历史记录列表")
    public R getMyHistoryList(@ApiIgnore @RequestAttribute("userId") Long userId) {
        System.out.println(userId);
        List list = myCourseService.getMyHistoryList(userId);
        return R.ok("", 100).put("data", list);
    }


    @GetMapping("{id}")
    @Login
    @ApiOperation("获得课程")
    public R learn(@PathVariable("id") Integer courseId, @ApiIgnore @RequestAttribute("userId") Long userId) {

        // Course course=courseService.getCourseById(id);
        Course c = courseService.getCourseById(courseId);
//        if(c.getIntegral()==0){
//            return R.ok("免费",100);
//        }
        Course course = myCourseService.getMyBuyCourseById(courseId, userId);
        if (course != null) {
            return R.ok("已购买", 100);
        } else {
            return R.ok("未购买", 100);
        }
    }

    @GetMapping("getCourseList")
    @Login
    @ApiOperation("获得我的课程列表")
    public R getCourseList(@ApiIgnore @RequestAttribute("userId") Long userId) {

        List list = myCourseService.getCourseList(userId);
        return R.ok().put("data", list);
    }

    @GetMapping("getMyCourseScheduleList")
    @Login
    @ApiOperation("获得我的浏览过课程列表")
    public R getMyCourseScheduleList(@ApiIgnore @RequestAttribute("userId") Long userId) {
        List list = myCourseService.getMyCourseScheduleList(userId);
        List notInMyPlanCourseIdList = coursePlanService.getCourseIdNotInMyPlan(userId);
        for (int s = 0; s < list.size(); s++) {
            CourseInfoAndPlanEntity courseInfoAndPlanEntity = (CourseInfoAndPlanEntity) list.get(s);
            courseInfoAndPlanEntity.setIsInPlan(1);
        }

        if (notInMyPlanCourseIdList.size() > 0) {
            for (int i = 0; i < notInMyPlanCourseIdList.size(); i++) {
                int courseId = Integer.valueOf(notInMyPlanCourseIdList.get(i).toString());
                if (list.size() > 0) {
                    for (int s = 0; s < list.size(); s++) {
                        CourseInfoAndPlanEntity courseInfoAndPlanEntity = (CourseInfoAndPlanEntity) list.get(s);
                        if (courseInfoAndPlanEntity.getId() == courseId) {
                            //判断不在计划中的课程id 和我浏览过的课程列表是不是匹配 匹配的话 修改CourseInfoAndPlanEntity中的isInPlan为2
                            courseInfoAndPlanEntity.setIsInPlan(2);
                        }
                    }
                }
            }
        }
        return R.ok().put("data", list);
    }

    @GetMapping("getScoreList")
    @Login
    @ApiOperation("获得我的成绩列表")
    public R getScoreList(@ApiIgnore @RequestAttribute("userId") Long userId) {

        List list = myCourseService.getCourseList(userId);
        return R.ok().put("data", list);
    }

    @PostMapping("buyMyCourse")
    @Login
    @ApiOperation("购买我的课程")
    public R buyMyCourse(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestBody Map map) {

        //List list=myCourseService.getCourseList(userId);
        //return R.ok().put("data", list);

        return myCourseService.buyMyCourse(userId, map);
    }


    @PostMapping("saveMyCourseSchedule")
    @Login
    @ApiOperation("保存我的课程记录")
    public R saveMyCourseSchedule(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestBody Map map) {
        map.put("userId", userId);
        CourseBody courseBody = null;
        CourseScheduleEntity courseScheduleEntity = myCourseService.getMCoursechedule(map);
        if (courseScheduleEntity != null) {
            if(null != map.get("courseBodyTime") && !"".equals(map.get("courseBodyTime").toString())){
                myCourseService.updateThisCourseSchedule(map);
                courseScheduleEntity.setCourseBodyTime((int)map.get("courseBodyTime"));
            }
        }
        if (null != courseScheduleEntity && null != courseScheduleEntity.getCourseBodyId()) {
            map.put("courseBodyId", courseScheduleEntity.getCourseBodyId());
            courseBody = courseService.getCourseBodyById(map);
            if (null != courseBody && null != courseScheduleEntity.getCourseBodyTime()) {
                courseBody.setVideoSchedule(courseScheduleEntity.getCourseBodyTime());
            }
            if (null != courseBody) {
                if (null != courseBody.getVideoPath()  && !"".equals(courseBody.getVideoPath())) {
                    String https = courseBody.getVideoPath().substring(0, 4);
                    if (https.equals("http")) {

                    }else{
                        courseBody.setVideoPath("https://" + courseBody.getVideoPath());
                    }
                }
                if (null != courseBody.getVideoHdPath() && !"".equals(courseBody.getVideoHdPath())) {
                    String https = courseBody.getVideoHdPath().substring(0, 4);
                    if (https.equals("http")) {

                    } else {
                        courseBody.setVideoHdPath("https://" + courseBody.getVideoHdPath());
                    }
                }
                if (null != courseBody.getCoverPath() && !"".equals(courseBody.getCoverPath())) {
                    String https = courseBody.getCoverPath().substring(0, 4);
                    if (https.equals("http")) {

                    } else {
                        courseBody.setCoverPath("https://" + courseBody.getCoverPath());
                    }
                }
                if (null != courseBody.getDataPath() && !"".equals(courseBody.getDataPath())) {
                    String https = courseBody.getDataPath().substring(0, 4);
                    if (https.equals("http")) {

                    } else {
                        courseBody.setDataPath("https://" + courseBody.getDataPath());
                    }
                }
            }
        }
        if (courseScheduleEntity == null) {
            myCourseService.saveMyCourseSchedule(map);
        }
        return R.ok("成功", 100).put("courseSchedule", courseScheduleEntity).put("courseBody", courseBody);
    }

    @PostMapping("updateMyCourseBodySchedule")
    @Login
    @ApiOperation("保存我的课程章节记录")
    public R updateMyCourseBodySchedule(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestBody Map map) {

        map.put("userId", userId);
        String courseBodyId = (String) map.get("courseBodyId");
       /* if (null == courseBodyId || "".equals(courseBodyId)) {
           return R.error(101,"未知章节");
        }*/
        if((null == courseBodyId || "".equals(courseBodyId))){
            return R.ok("",100);
        }
        CourseBodyScheduleEntity courseScheduleEntity = myCourseService.getMyCourseBodyScheduleEntity(map);
        if (courseScheduleEntity == null) {
            myCourseService.saveMyCourseBodyScheduleEntity(map);
        }

        myCourseService.updateMyCourseBodySchedule(map);
        return R.ok("成功", 100);
    }
}

